import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { ElMessageBox, ElMessage } from "element-plus";


const useManagementStore = defineStore('management', () => {
    const managements = ref([
        {
            id: '1',
            name: '张三',
            work: '销售',
            miaoshu: '每日销售',
            bumen: '销售部',
            use: '是',
            time: '2022-10-26'
        },
        {
            id: '2',
            name: '张三',
            work: '销售',
            miaoshu: '每日销售',
            bumen: '销售部',
            use: '是',
            time: '2022-10-26'
        }, {
            id: '3',
            name: '张三',
            work: '销售',
            miaoshu: '每日销售',
            bumen: '销售部',
            use: '是',
            time: '2022-10-26'
        }, {
            id: '4',
            name: '张三',
            work: '销售',
            miaoshu: '每日销售',
            bumen: '销售部',
            use: '是',
            time: '2022-10-26'
        },
    ])
    // 增加一条数据
    const addMans = (obj) => {
        const iss = managements.value.find(item => item.name == obj.name)
        if (iss) {
            // console.log(obj)
            iss.name = obj.name
            iss.bumen = obj.bumen
            iss.miaoshu = obj.miaoshu
            iss.use = obj.use
            ElMessage('修改成功')
        } else {
            ElMessage('添加成功')
            // console.log(obj)
            managements.value.push(obj)
        }

    }
    // 编辑数据回显
    const delMans = (id) => {
        return managements.value.find((item, index) => item.id == id)

    }
    // 删除一天数据
    const removeMans = (id) => {
        const index = managements.value.findIndex((item, index) => item.id == id)
          managements.value.splice(index, 1)
    }
    return {
        managements,
        addMans,
        removeMans,
        delMans
    }
}, {

    persist: true
})

export default useManagementStore